//
//  HHSpeechController.swift
//  01-LearnAVFoundation
//
//  Created by hehuafeng on 2017/5/16.
//  Copyright © 2017年 雷厉峰行. All rights reserved.
//

import UIKit
import AVFoundation

class HHSpeechController: NSObject {

    // MARK: - 属性
    fileprivate lazy var synthesizer : AVSpeechSynthesizer = {
        let synthesizer = AVSpeechSynthesizer()
        return synthesizer
    }()
    
    // 懒加载语音播放器数组
    fileprivate lazy var voices : [AVSpeechSynthesisVoice] = {
        let voices : [AVSpeechSynthesisVoice] = [AVSpeechSynthesisVoice(language: "en-US")!, AVSpeechSynthesisVoice(language: "en-GB")!]
        return voices
    }()
    
    // 懒加载文字数组
    fileprivate lazy var speechStrings : [String] = ["Hello AV Foundation. How are you?", "I'm well!, Thanks for asking", "Are you excited about the book?"]
    
    func beginConversation() {
        for i in 0..<speechStrings.count {
            // 1. 创建语音播放内容对象
            let utterance = AVSpeechUtterance(string: speechStrings[i])
            
            // 2. 设置属性
            utterance.voice = voices[i % 2]  // 设置播放语音
            utterance.rate = 0.4  // 设置播放语速
            utterance.pitchMultiplier = 0.8  // 设置播放声音音调
            utterance.postUtteranceDelay = 0.1  // 设置两句语音之间的延时时间
            synthesizer.speak(utterance)  // 播放语音
        }
    }
}
